package core.collection.mycollection;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Collections常用方法
 *
 * @author
 * @create 2018-12-23 17:15
 **/
public class mycollection_7_Collections {
    /**
     * 	Collecitons中的常见方法
     * 	public static <T> void sort(List<T> list)
        public static <T> int binarySearch(List<?> list,T key)
        public static <T> T max(Collection<?> coll)
        public static void reverse(List<?> list)
        public static void shuffle(List<?> list)
     */
    public static void main(String[] args) {
        //demo_sort();
        //demo_binarySearch();
        //demo_max();
        //demo_reverse();
        demo_shuffle();
    }

    public static void demo_sort(){
        ArrayList<String> list = new ArrayList<>();
        list.add("b");
        list.add("a");
        list.add("d");
        list.add("n");
        list.add("c");
        list.add("h");
        list.add("r");
        System.out.println(list);

        Collections.sort(list);
        System.out.println(list);
    }

    public static void demo_binarySearch(){
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("f");
        list.add("g");
        System.out.println(Collections.binarySearch(list,"c"));
        System.out.println(Collections.binarySearch(list,"b"));
        //否则返回负的插入点减一
    }

    public static void demo_max(){
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("f");
        list.add("g");
        System.out.println(Collections.min(list));
        System.out.println(Collections.max(list));
    }

    public static void demo_reverse(){
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("f");
        list.add("g");
        Collections.reverse(list);
        System.out.println(list);
    }

    public static void demo_shuffle(){
        //洗牌 随机置换
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("f");
        list.add("g");
        Collections.shuffle(list);
        System.out.println(list);
    }
}
